<?php

namespace app\backend\controller;

use app\common\model\Money;
use app\common\model\Withdrawal as withdrawalModel;
use app\common\model\User;
use app\common\model\WalletLog;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;

class Withdrawal extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function withdrawalList()
    {
        $limit = request()->param('limit');
        $find = request()->param('find');
        if($find){
            $list = withdrawalModel::where('user_id',$find)->order('status asc')->order('id desc')->paginate($limit);
            $count = withdrawalModel::where('user_id',$find)->count();

        }else{
            $list = withdrawalModel::order('status asc')->order('id desc')->paginate($limit);
            $count = withdrawalModel::count();
        }
        $status = ['未审核','审核成功','审核失败'];
        foreach($list as $key=>$value){
            $list[$key]['status1'] = $status[$list[$key]['status']];
        }
        if ($list) {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
            return json($data);
        } else {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => array()];
            return json($data);
        }

    }
    //批量审核成功
    public function review_success(){
        $id = request()->param('id');
       foreach($id as $value){
           $info = withdrawalModel::where('id',$value)->find();
           if($info['status'] == 0){
               //审核成功提现，改变状态
               //到时候接自动提现到微信零钱的接口
               $res = withdrawalModel::where('id',$value)->update(['status'=>1]);
           }else{
               $res = 1;
           }
       }
        return array('code'=>'1','msg'=>'操作成功');

    }
    //单条审核成功
    public function review_suc(){
        $id = request()->param('id');
        $info = withdrawalModel::where('id',$id)->find();
        if($info['status'] == 0){
            //审核成功提现，改变状态
            //到时候接自动提现到微信零钱的接口
            $res = withdrawalModel::where('id',$id)->update(['status'=>1]);
            if($res){
                return array('status'=>'1','msg'=>'操作成功');
            }else{
                return array('status'=>'0','msg'=>'操作失败');
            }
        }else{
            $res = 1;
        }
        return array('status'=>'1','msg'=>'操作成功');
    }
    //单条审核失败
    public function review_err(){
        $id = request()->param('id');
        $info = withdrawalModel::where('id',$id)->find();
        if($info['status'] == 0){
            //审核成功失败，改变状态，返还金额到用户
            $res = withdrawalModel::where('id',$id)->update(['status'=>2]);
            if($res){
                //添加余额记录
                $data = [
                    'userid' => $info['user_id'],
                    'money' => $info['money'],
                    'type' => 2,//提现失败返还
                ];
                $result = WalletLog::create($data);
                if($result){
                    Money::where('userid', $info['user_id'])->setInc('balance',$info['money']);
                }
                return array('status'=>'1','msg'=>'操作成功');
            }else{
                return array('status'=>'0','msg'=>'操作失败');
            }
        }else{
            $res = 1;
        }
        return array('status'=>'1','msg'=>'操作成功');
    }
}
